import 'package:flutter/material.dart';
import 'package:flutter_demo/core/common/utils/image_utils.dart';
import 'package:flutter_demo/core/common/values/gaps.dart';
import 'package:flutter_demo/ui/pages/treasure/module/course_item.dart';
import 'package:flutter_demo/ui/pages/treasure/module/lt_scene_vos.dart';
import 'package:flutter_demo/ui/pages/treasure/module/query_home_page.dart';
import 'package:flutter_demo/ui/pages/treasure/widgets/article_widget.dart';
import 'package:flutter_demo/ui/pages/treasure/widgets/course_widget.dart';
import 'package:flutter_demo/ui/pages/treasure/widgets/key_word_widget.dart';
import 'package:flutter_demo/ui/pages/treasure/widgets/search_appbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

import 'index.dart';

//https://www.bilibili.com/video/BV15G411j7X2/
class TreasurePage extends GetView<TreasureController> {
  static const routeName = "/TreasurePage";

  const TreasurePage({Key? key}) : super(key: key);

  // 主视图 错误实例
  Widget _buildView() {
    return SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Column(
          children: [
            SizedBox(
              height: 211.h,
              //Stack 撑不起来父组件， 需要父组件定义大小
              child: Stack(children: [
                Image.asset(
                  ImageUtils.getImagePath('2', format: "jpg"),
                  height: 210.h,
                  //填满空间
                  fit: BoxFit.cover,
                ),
                Positioned.fill(
                    top: 180.h,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white.withOpacity(1),
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(12.r),
                            topRight: Radius.circular(12.r)),
                      ),
                    )),
                Positioned(
                  right: 38.w,
                  top: 155.h,
                  child: SearchAppbar(),
                )
              ]),
            ),

            Container(
              decoration: BoxDecoration(
                color: Colors.white.withOpacity(1),
              ),
              child: Column(
                children: [
                  //KeyWordWidget(null),
                  Gaps.line20_3,
                  //const CouresWidget(),
                  SizedBox(
                    height: 500.h,
                    child: ListView.separated(
                      shrinkWrap: true,
                      scrollDirection: Axis.vertical,
                      padding: EdgeInsets.only(left: 15.w, right: 15.w),
                      itemBuilder: (context, index) {
                        return Text("------");
                      },
                      separatorBuilder: (context, index) {
                        return Gaps.hGap9;
                      },
                      itemCount: 100,
                    ),
                  )
                ],
              ),
            ),
            //color: Colors.blueGrey,
          ],
        ));
  }

// 主视图
  Widget _buildView2(BuildContext context, TreasureState? state) {
    final listScroll = ScrollController();

    return Container(
        decoration: BoxDecoration(
          color: Colors.white.withOpacity(1),
        ),
        child: SmartRefresher(
          controller: controller.refreshController,
          enablePullUp: true,
          onRefresh: () {
            controller.onRefreshPage();
          },
          onLoading: () {
            controller.onMorePage();
          },
          child: ListView.separated(
            //shrinkWrap: true,
            scrollDirection: Axis.vertical,
            controller: listScroll,
            // padding: EdgeInsets.only(left: 15.w, right: 15.w),
            itemBuilder: (context, index) {
              if (index == 0) {
                return buildWidget();
              }
              QueryHomePage homePage = controller.state.vo!;
              //关键词
              if (index == 1) {
                final list = <Widget>[];
                List<LtSceneVOS> lst = homePage.ltSceneVOS ?? [];
                lst.forEach((e) {
                  list.add(KeyWordWidget(e));
                });
                return Column(
                  children: list,
                );
              }
              //优选科创
              if (index == 2) {
                List<CourseItem> combatList = homePage.combatList ?? [];
                return CouresWidget(combatList);
              }

              List<CourseItem> courseList = homePage.courseList!;

              return ArticleWidget(courseList[index - 3], index: index - 3);
            },
            separatorBuilder: (context, index) {
              if (index == 1 || index == 2) {
                return Gaps.line20_3;
              }
              return Gaps.hGap9;
            },
            itemCount: ((controller.state.vo?.courseList?.length ?? 0) + 3),
          ),
          //color: Colors.blueGrey,
        ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //appBar: AppBar(title: const Text("treasure")),
      //SafeArea 可以去除遮挡
      body: SafeArea(
        child: GetBuilder<TreasureController>(builder: (controller) {
          if (controller.state.status.value == "1") {
            return Center(
              child: TextButton(
                  onPressed: () {
                    controller.onRefreshPage();
                  },
                  child: const Text(
                    "网络可能点问题，点我刷新一下",
                    style: TextStyle(
                      color: Colors.black,
                    ),
                  )),
            );
          } else if (controller.state.status.value == "3") {
            controller.refreshController.refreshFailed();
            return Gaps.empty;
          } else if (controller.state.status.value == "2") {
            return const Text("plase init -----");
          } else {
            return _buildView2(context, controller.state);
          }
        }),
      ),
    );
  }

  Widget buildWidget() {
    return SizedBox(
      height: 211.h,
      //Stack 撑不起来父组件， 需要父组件定义大小
      child: Stack(children: [
        Image.asset(
          ImageUtils.getImagePath('2', format: "jpg"),
          height: 210.h,
          //填满空间
          fit: BoxFit.cover,
        ),
        Positioned.fill(
            top: 180.h,
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white.withOpacity(1),
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(12.r),
                    topRight: Radius.circular(12.r)),
              ),
            )),
        Positioned(
          right: 38.w,
          top: 155.h,
          child: SearchAppbar(),
        )
      ]),
    );
  }
}
